/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : May 10, 2012  3:28:01 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.construction.purchase.request
 * File Name : PurchaseOrderRequestProcessing.java
 * TODO
 */
package apps.business.process.construction.purchase.request;

import java.util.List;
import jpa.entitys.synchro.staging.MmPurchaseRequest;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import apps.business.process.SyncProcessorCommons;
import apps.promis.out.common.PurchaseRequestAction;
import sap.business.mm.outbound.SyncPrItemService;

/**
 * @author chatree
 *
 */
public class PurchaseRequestProcessing extends SyncProcessorCommons {
	
	private static final Logger log = Logger.getLogger(PurchaseRequestProcessing.class);
	
	/**
	 * 		String company = "SP";
			String project = "SPLVILLE-PRAGSA"; //SPLVILLE-PRAGSA//SPLCITY-SRI
			String documentNo = "PM-1206-000002";
			String status = "A";
			String syncIsstaging = "N";
	 * @param company
	 * @param project
	 * @param documentNo
	 * @param status
	 * @param syncIsstaging
	 * @throws Exception
	 */
	public void Processing(String company, String project, String documentNo, String status, String syncIsstaging) throws Exception {
		try{
			
			//CreateTo Staging
			
			UPD_PROMISUseTime(true, false, null);
			List<MmPurchaseRequest> value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
			UPD_PROMISUseTime(false, true, null);
			System.out.println("Purchase Request To Staging size=" + CommonService.getSize(value_list));
			
			//Send to SAP
			if(CommonService.getSize(value_list) > 0){
				UPD_SENDUseTime(true, false, value_list.get(0).getInstanceNo());
				new SyncPrItemService().SendingToSAP(value_list);
				UPD_SENDUseTime(false, true, value_list.get(0).getInstanceNo());
			}			
			System.out.println("Sending Purchase Request To SAP size=" + CommonService.getSize(value_list));
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;			
		}
	}
	
	public void ProcessingPRAllInStagingIsSync(String ISSYNC, String SYNC_CREATEDON) throws Exception {
		try{
			
			List<Object[]> lst = new PurchaseRequestAction().SearchPRAllInStagingIsSync(ISSYNC, SYNC_CREATEDON);
			System.out.println("SearchPRAllInStagingIsSync size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					
					String company = value[0].toString();
					String project = value[1].toString();
					String documentNo = value[2].toString();
					String status = value[3].toString();
					String syncIsstaging = "N";
					
					//Create to Staging
					List<MmPurchaseRequest> value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
					System.out.println("Create Purchase Request To Staging Size=" + CommonService.getSize(value_list));
					
					//Send to SAP
					new SyncPrItemService().SendingToSAP(value_list);
					System.out.println("Sending Purchase Request To SAP Size=" + CommonService.getSize(value_list));
				}
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	public void ProcessingNotSyncInStaging() throws Exception {
		try{
			List<Object[]> lst = new PurchaseRequestAction().SearchNotSyncInStaging();
			log.info("ProcessingNotSyncInStaging size=" + CommonService.getSize(lst));
			
			if(CommonService.getSize(lst) > 0){
				for(Object[] value : lst){
					try{
						
						String instanceNo = value[3].toString();
						List<MmPurchaseRequest> prlist = new PurchaseRequestAction().findByInstanceNo1(instanceNo);
						
						//Send to SAP
						new SyncPrItemService().SendingToSAP(prlist);
						System.out.println("Sending Purchase Request To SAP Size=" + CommonService.getSize(prlist));
						log.info("Sending Purchase Request To SAP Size=" + CommonService.getSize(prlist));
						
					}catch(Exception e){
						System.out.println(e.getMessage());
						log.error(e.getMessage());
					}
				}
			}
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
	}
	
	private void ProcessingIsnotSync(){
		try{
			StringBuilder _strQuery = new StringBuilder();
			_strQuery.append("  SELECT COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING , CREATED_BY, CREATED_ON , IS_INTERFACE, SYNC_ISSYNC ");
			_strQuery.append("  FROM SPL.VW_SYNC_PR ");
			_strQuery.append("  WHERE STATUS IN ('A', 'Z') ");
			_strQuery.append("  AND SYNC_ISSTAGING = 'N' ");
			_strQuery.append("  AND IS_INTERFACE = 'Y' ");
			_strQuery.append("  GROUP BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING , CREATED_BY, CREATED_ON, IS_INTERFACE, SYNC_ISSYNC ");
			_strQuery.append("  ORDER BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING , CREATED_BY, CREATED_ON, IS_INTERFACE, SYNC_ISSYNC ");
			
			
			
		}catch(Exception e){
			System.err.println(e.getMessage());
			log.error(e.getMessage());			
		}
	}
	
	@Deprecated
	private void Start() throws Exception {
		try{
			
			//Asset  PM-1205-2
			//Misc   PM-1205-3
			//Misc   PR-1205-11
			
			//High Rise
			//Cons  PR-1205-2
			//Misc   PM-1205-4
			//Asset  PM-1205-5
					 
			String company = "SP";
			String project = "SVSP"; 			//SPLVILLE-PRAGSA//SPLCITY-SRI
			String documentNo = "PR-1210-000004";
			String status = "A";
			String syncIsstaging = "N";
			
			//CreateTo Staging			
			List<MmPurchaseRequest> value_list = new PurchaseRequestAction().CreateToStaging(company, project, documentNo, status, syncIsstaging);
			
			//Send to SAP
			new SyncPrItemService().SendingToSAP(value_list);
			log.info("Sending Purchase Request To SAP  size="+ CommonService.getSize(value_list) +" --->");
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try{
			PurchaseRequestProcessing obj = new PurchaseRequestProcessing();
			obj.Start();

		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}

}
